#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDebug>
#include <QVector>
#include <QMessageBox>
#include "mywidgetfactory.h"
#include "mytcpproxy.h"

//QVector<QVector<QString>> tableDatas={{"客厅的设备","XX-00001","在线","无"},
//                                      {"卧室的设备","XX-00002","离线","无"},
//                                      {"阳台的设备","XX-00003","在线","无"}};



MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
}

MainWidget::~MainWidget()
{
    delete ui;
}

//重写show函数
void MainWidget::show(QString userid)
{
    QWidget::show();
    this->userid = userid;
    //获取在线设备列表
    MyTcpProxy::sndGetDevList(userid);
}

//Tab1: 表格双击事件
void MainWidget::on_tableWidget_cellDoubleClicked(int row, int column)
{
    (void)column;
    //将值填充到右侧面板中
    ui->input_devname->setText(tableDatas_[row][0]);
    ui->input_devid->setText(tableDatas_[row][1]);
    ui->label_state->setText(tableDatas_[row][2]);
}

//Tab1: 开始按钮
void MainWidget::on_pushButton_2_clicked()
{
    qDebug() << "start";
    MyTcpProxy::sndStartCamera(userid, ui->input_devid->text());
}

//Tab1: 结束按钮
void MainWidget::on_pushButton_clicked()
{
//    qDebug() << "end";
//    MyTcpProxy::sndStopCamera(userid, ui->input_devid->text());

    qDebug() << "socket avail cnt: " << MyTcpProxy::getAvailCnt();
}

//Tab2: 删除设备按钮
void MainWidget::on_pushButton_4_clicked()
{
    qDebug() << "删除设备";
    QMessageBox::information(this,"提示","双击左侧表格以删除设备");
}

//Tab2: 添加设备按钮
void MainWidget::on_pushButton_3_clicked()
{
    qDebug() << "添加设备";
    this->hide();
    MyWidgetFactory::getAddDeviceWidget()->show(userid);
}

//Tab2: 双击事件
void MainWidget::on_tableWidget_2_cellDoubleClicked(int row, int column)
{
    (void)column;
    qDebug() << "table delete";
    //
    if(QMessageBox::critical(this,"删除设备","你确定要删除"+tableDatas_[row][0]+"吗？",
                             QMessageBox::Ok,QMessageBox::Cancel)==QMessageBox::Ok){
        qDebug() << "删除设备";
        MyTcpProxy::sndDevDel(userid, tableDatas_[row][1]);
    }else{
        qDebug() << "do nothing";
    }
}

void MainWidget::onDevListRet(const bool &ret, QVector<QVector<QString> > &tableData)
{
    if(!ret){
        QMessageBox::critical(this,"设备列表查询","失败：可能存在通信故障");
        return;
    }
    this->tableDatas_ = tableData;
    //刷新界面
    refreshTableUI();
    if(tableDatas_.size() == 0){
        QMessageBox::information(this,"提示","您还没有绑定设备，请至<我的设备>界面绑定设备");
    }
}

void MainWidget::onDevDelRet(const bool &ret, const QString &msg)
{
    if(ret){
        QMessageBox::information(this,"设备删除","设备删除成功");
        MyTcpProxy::sndGetDevList(userid);//刷新表格
        return;
    }else{
        QMessageBox::critical(this,"设备删除","设备删除失败: "+ msg);
    }
}

void MainWidget::onStartCameraRet(const bool &ret, const QString &msg)
{
    if(ret){
        QMessageBox::information(this,"开启远程摄像头","命令已发送");
        return;
    }else{
        QMessageBox::critical(this,"开启远程摄像头","开启失败： "+msg);
        return;
    }
}

void MainWidget::onStopCameralRet(const bool &ret, const QString &msg)
{
    if(ret){
        QMessageBox::information(this,"关闭远程摄像头","命令已发送");
        return;
    }else{
        QMessageBox::critical(this,"关闭远程摄像头","关闭失败： "+msg);
        return;
    }
}

void MainWidget::onVideoRecv(const QPixmap &pic)
{
    qDebug() << "收到拍摄的图片信息";
    QPixmap pic2=pic.scaled(ui->label_display->size());
    ui->label_display->setPixmap(pic2);
}

void MainWidget::refreshTableUI()
{
    while(ui->tableWidget->rowCount() > 0)
        ui->tableWidget->removeRow(0);

    //表格1 属性
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setColumnWidth(0,60);
    ui->tableWidget->setColumnWidth(1,180);

    //表格1: 的title
    ui->tableWidget->setHorizontalHeaderLabels({"序号","设备名称","状态"});

    //表格1：清空之前的内容

    //表格1: 的内容
    for(int i=0;i<tableDatas_.size();i++){
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
        QTableWidgetItem *item = new QTableWidgetItem(QString(i+1+'0'));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,0,item);
        //
        item = new QTableWidgetItem(tableDatas_[i][0]);
                item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,1,item);
        //
        item = new QTableWidgetItem(tableDatas_[i][2]);
                item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,2,item);
    }

    //Tab2的表格展示
    while(ui->tableWidget_2->rowCount() > 0)
        ui->tableWidget_2->removeRow(0);

    ui->tableWidget_2->setColumnCount(5);
    ui->tableWidget_2->setHorizontalHeaderLabels({"序号","设备名称","设备ID","状态","备注"});

    for(int i=0;i<tableDatas_.size();i++){
        ui->tableWidget_2->insertRow(ui->tableWidget_2->rowCount());
        //
        QTableWidgetItem *item = new QTableWidgetItem(QString(i+1+'0'));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget_2->setItem(i,0,item);
        //
        for(int j=1;j<5;j++){
            QTableWidgetItem *item = new QTableWidgetItem(tableDatas_[i][j-1]);
            item->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget_2->setItem(i,j,item);
        }
    }
}

void MainWidget::on_pushButton_5_clicked()
{
    MyTcpProxy::sndGetDevList(userid);
}
